package manager.web.contrl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import framework.util.validatecode.ValidateCode;
import manager.domain.SessionParamEnum;
import manager.web.domain.IndexResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.ModelAndView;
import pub.permission.domain.ExtSysResource;
import pub.permission.domain.Resource;
import pub.permission.service.ExtSysResourceService;
import pub.permission.service.ResourceService;
import pub.user.domain.User;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 描述:
 *
 * @author 002465
 * @created 2017/5/3 21:46
 * @return ${return_type}
 * @since v1.0.0
 * ${tags}
 */
@Controller
@RequestMapping
public class IndexCtrl {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private ExtSysResourceService extSysResourceService;
    @RequestMapping(value = "/index")
    public String index(ModelMap modelMap, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(
                SessionParamEnum.USER.getValue());
        if(user==null){
            return "view/login";
        }
        // 权限菜单初始化
        /*List<Resource> resources = resourceService.getResourceTree(user);
        modelMap.put("resources", resources);*/
        List<ExtSysResource> extSysResourceList = extSysResourceService.getManageMenuList();
        Gson gson = new GsonBuilder().serializeNulls().create();
        IndexResult result = getResult(extSysResourceList);
        modelMap.put("result", gson.toJson(result));

        String ctx = request.getContextPath();
        modelMap.put("ctx", ctx);
        modelMap.put("staticPath", ctx+"/static");
        /*modelMap.addAttribute("account",account);
        IndexResult result = getResult(accountEntity);
        Gson gson = new GsonBuilder().serializeNulls().create();
        modelMap.put("result", gson.toJson(result));*/
        return "view/index";
    }
    private IndexResult getResult(List<ExtSysResource> extSysResourceList){
        IndexResult result = new IndexResult();
       /* Resource rootNode = resourceService.getRootNodeByAccount(account);
        result.setMenuData(rootNode);*/
        ExtSysResource rootNode = new ExtSysResource();
        rootNode.setChildren(extSysResourceList);
        result.setMenuData(rootNode);
        return result;
    }
    @RequestMapping(value = "/getValidateImage")
    @ResponseBody()
    public void getValidateImage(HttpServletRequest request,
                                 HttpServletResponse response) {
        try {
            ValidateCode vCode = new ValidateCode(150, 40, 4, 100);
            ServletOutputStream output = response.getOutputStream();
            vCode.write(output);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
